#include "cm_registryitem.h"

CM_RegistryItem::CM_RegistryItem(void)
{
}

CM_RegistryItem::~CM_RegistryItem(void)
{
}

int 
CM_RegistryItem::saveRegistries(int collectionId, WMI_Result<WMI_Registry>& registries)
{
    try 
    {
        WMI_Registry* regParent = registries.getParentRegistry();
        sqlite3_connection con(DEFAULT_DATABASE_NAME.c_str());

        sqlite3_transaction trans(con);
        saveRegistry(0, collectionId, regParent, con);
        trans.commit();

        int regParentId = getRegistryLastId();        

        if ( regParentId > 0)
        {
            sqlite3_transaction trans2(con);            
            vector<WMI_Registry*>& childsVector = regParent->getRegistryList();
            for(vector<WMI_Registry*>::const_iterator childIt = childsVector.begin(); childIt != childsVector.end(); childIt++ )
            {
                saveRegistry(regParentId, collectionId, (*childIt), con);
            }            

            trans2.commit();

            vector<WMI_Value>& valueVector = regParent->getValueList();
            for(vector<WMI_Value>::iterator valueIt = valueVector.begin(); valueIt != valueVector.end(); valueIt++ )
            {
                saveValue(regParentId, (*valueIt), con);
            } 
        }       		
        
        con.close();
    }
    catch(exception &ex) 
    {
        cerr << "Exception Occured: saveRegistries(...): " << ex.what() << endl;
        return 0;
    }

    return 1;
}

int 
CM_RegistryItem::getRegistries(int collectionId, WMI_Result<WMI_Registry>& registries)
{
    try 
    {
        sqlite3_connection con(DEFAULT_DATABASE_NAME.c_str());
        {           
            sqlite3_command cmd(con, SELECT_REGISTRYPARENT_SCRIPT);
            cmd.bind(1, 0);
            cmd.bind(2, collectionId);
            sqlite3_reader reader = cmd.executereader();

            WMI_Registry* regParent = new WMI_Registry;
            int regParentId = -1;
            wstring *aux;
            if(reader.read())
            {	
                regParentId = reader.getint(0);
                aux = new wstring(reader.getstring16(3));
                regParent->setName(*aux);
                //regParent->setPath(reader.getstring16(4));               
            }

//			delete aux;

            if( regParentId > 0)
            {
                registries.setWRegistry(regParent);
                sqlite3_command cmd2(con, SELECT_REGISTRY_SCRIPT);
                cmd2.bind(1, regParentId);
                sqlite3_reader reader2 = cmd2.executereader();

                WMI_Registry* registryChild;
                while(reader2.read())
                {	
                    registryChild = new WMI_Registry;
                    aux = new wstring(reader2.getstring16(3));
                    registryChild->setName(*aux);
                    //registryChild->setPath(reader2.getstring16(4)); 
                    regParent->addRegistry(registryChild);
                }

                sqlite3_command cmd3(con, SELECT_VALUE_SCRIPT);
                cmd3.bind(1, regParentId);
                sqlite3_reader reader3 = cmd3.executereader();

//				delete aux;

				wstring *aux2;
                while(reader3.read())
                {
                    WMI_Value value;
                    aux = new wstring(reader3.getstring16(2));
                    value.setName(*aux);
                    aux2 = new wstring(reader3.getstring16(3));
                    value.setType(*aux2);                    
                    regParent->addValue(value);
                }
            }                       
        }

        con.close();
    }
    catch(exception &ex) {
        cerr << "Exception Occured: getRegistries() " << ex.what() << endl;
        return 0;
    }

    return 1;
}

int 
CM_RegistryItem::getRegistryLastId()
{
    int registryId = -1;

    try {
        sqlite3_connection con(DEFAULT_DATABASE_NAME.c_str());
        {
            sqlite3_command cmd(con, "select max(registry_id) from registry ");
            sqlite3_reader reader = cmd.executereader();

            if(reader.read()){
                registryId = reader.getint(0);				
            }
        }

        con.close();
    }
    catch(exception &ex) {
        cerr << "Exception Occured: getRegistryLastId():" << ex.what() << endl;
        return 0;
    }

    return registryId;
}

int 
CM_RegistryItem::saveRegistry(int parentId, int collectionId, WMI_Registry* registry, sqlite3_connection& con)
{
    try 
    {
        sqlite3_command cmd(con, INSERT_REGISTRY_SCRIPT);
        cmd.bind(1, parentId);
        cmd.bind(2, collectionId);
        cmd.bind(3, registry->getName());
        cmd.bind(4, registry->getPath());
        cmd.executenonquery();       
    }
    catch(exception &ex) 
    {
        cerr << "Exception Occured: saveRegistry(): " << ex.what() << endl;
        return 0;
    }

    return 1;
}

int 
CM_RegistryItem::saveValue(int registryId, WMI_Value& value, sqlite3_connection& con)
{
    try 
    {
        sqlite3_command cmd(con, INSERT_VALUE_SCRIPT);
        cmd.bind(1, registryId);
        cmd.bind(2, value.getName());
        cmd.bind(3, value.getType());
        cmd.executenonquery();       
    }
    catch(exception &ex) 
    {
        cerr << "Exception Occured: saveValue(): " << ex.what() << endl;
        return 0;
    }

    return 1;
}
